//
//  GroupButton.m
//  HTP
//
//  Created by admin on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "GroupButton.h"
static NSMutableDictionary *_groupButtonDic = nil;
@interface GroupButton()
@property (nonatomic,retain) UIButton *selectBtn;
@property (nonatomic,retain) UILabel  *textLabel;
@end
@implementation GroupButton
@synthesize delegate = _delegate;
@synthesize isSelect = _isSelect;
-(id)initWithDelegate:(id)delegate groupId:(NSString *)groupId buttonImage:(NSString *)image unSelectImage:(NSString *)unSelectImage title:(NSString *)title withFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        _delegate = delegate;
        _groupId = [groupId copy];
        _selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame)/2.0 - 12.5, 10, 25, 25)];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:unSelectImage] forState:UIControlStateNormal];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
//        [_selectBtn addTarget:self action:@selector(YHBtnCheckeds) forControlEvents:UIControlEventTouchUpInside];
        _selectBtn.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_selectBtn];
        
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_selectBtn.frame)+5, CGRectGetWidth(frame), CGRectGetHeight(frame) / 3.0-5)];
        _textLabel.textAlignment = _TEXT_ALIGN_CENTER;
        _textLabel.text = title;
        _textLabel.font = [UIFont systemFontOfSize:12];
        _textLabel.textColor = RGBCOLOR(0, 0, 0, 0.7);
        
        
        [self addSubview:_textLabel];
        UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        [backBtn addTarget:self action:@selector(YHBtnCheckeds) forControlEvents:UIControlEventTouchUpInside];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [self addSubview:backBtn];
        
        [self addToGroup];
    }
    return self;
}
//TODO:单例模式记录添加的view
-(void)addToGroup{
    if (!_groupButtonDic) {
        _groupButtonDic = [[NSMutableDictionary dictionary] retain];
    }
    NSMutableArray *_btns = [_groupButtonDic objectForKey:_groupId];
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    [_btns addObject:self];
    [_groupButtonDic setObject:_btns forKey:_groupId];
}
/**
 *  对应button移除
 */
-(void)removeFromGroup{
    if (_groupButtonDic) {
        NSMutableArray *_btns = [_groupButtonDic objectForKey:_groupId];
        if (_btns) {
            [_btns removeObject:self];
            if (_btns.count == 0) {
                [_groupButtonDic removeObjectForKey:_groupId];
            }
        }
    }
}
/**
 * self 被设置为选中状态
 */
-(void)YHBtnCheckeds{
 
        if (_isSelect) {
            return;
        }
        _selectBtn.selected = !_selectBtn.selected;
        _isSelect = _selectBtn.selected;
    
        if (_selectBtn.selected) {
            [self yHUnCheckOtherView];
            [self setSelectState];
            //把其他group 设置为非选择状态
        }
    
    if (_selectBtn.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectGroup:groupId:)]) {
        [_delegate didSelectGroup:self groupId:_groupId];
    }
    
}
/**
 *  同组内修改选中状态
 */
-(void)yHUnCheckOtherView{
    NSMutableArray *btns = [_groupButtonDic objectForKey:_groupId];
    if (btns!=nil   &&  btns.count > 0) {
        [btns enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            GroupButton *yhBtn = (GroupButton *)obj;
            if (yhBtn.isSelect && ![yhBtn isEqual:self]) {
                yhBtn.isSelect = NO;
            }
        }];
    }
}
/**
 *
 *  记录self 是否被选中
 *  @param isSelect
 */
-(void)setIsSelect:(BOOL)isSelect{
    if (_isSelect == isSelect) {
        return;
    }
    _isSelect = isSelect;
    _selectBtn.selected = _isSelect;
    
    if (_selectBtn.selected) {
        [self yHUnCheckOtherView];
        [self setSelectState];
    }else{
        [self setUnSelectState];
    }
    
    if (_selectBtn.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectGroup:groupId:)]) {
        [_delegate didSelectGroup:self groupId:_groupId];
    }
    
}
-(void)setSelectState{
    [_textLabel setTextColor:[UIColor orangeColor]];

}
-(void)setUnSelectState{
    [_textLabel setTextColor:RGBCOLOR(0, 0, 0, 0.7)];
}
//TODO:非arc
-(void)dealloc{
    [self removeFromGroup];
    
    _delegate = nil;
    [_groupId release];
    _groupId = nil;
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
